<?php
namespace Admin\Controller;
use Admin\Model\UserLogModel;

class CartController extends MainController{
    /**
     * 添加购物车
     * @queyuan
     * 2016/07/02
     * @param $sale_id,$user_id,$num,$ip
     */
    public function addCart(){
        $sale_id = I("param.sale_id");
        $user_id = I("param.user_id");
        $num = I("param.num");
        $ip = I("param.ip");
        $result = array();
        if(!$sale_id || !$user_id || !$num || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id." and status != 0")->find();
            if(!$user){
                $result['status'] = 1;
                $result['msg'] = "无此用户";
            }else{
                $sale = M("sales")->where("id = ".$sale_id)->field("need_num,sale_num,(need_num-sale_num) as left_num,min_buy")->find();
                $cart = M("cart")->where("user_id = ".$user_id." and sale_id = ".$sale_id." and status = 0")->field("num,id")->find();
                if(!$cart){
                    $cart = array('num'=>0,'id'=>0);
                }
                if(!$sale){
                    $result['status'] = 1;
                    $result['msg'] = "无此在售商品";
                }else{
                    $left = $sale['left_num'] - $cart['num'];
                    if($left < $sale['min_buy']){
                        $result['status'] = 1;
                        $result['msg'] = "库存不够了";
                    }else{
                        if($cart['id']){
                            $data = array('num'=>($cart['num']+$sale['min_buy']));
                            $res = M("cart")->where("id = ".$cart['id'])->save($data);
                        }else{
                            $data = array(
                                'user_id'=>$user_id,
                                'sale_id'=>$sale_id,
                                'num'=>$sale['min_buy'],
                                'status'=>0,
                                'add_time'=>time(),
                            );
                            $res = M("cart")->add($data);
                        }
                        if($res){
                            $log = new UserLogModel();
                            $log->addLog($user['id'],"用户：".$user['name']."添加购物车",0,$ip,"cart");
                            $count = M("cart")->where("user_id = ".$user_id." and status = 0")->count();
                            $result['status'] = 0;
                            $result['msg'] = "添加成功";
                            $result['count'] = $count;
                        }else{
                            $result['status'] = 1;
                            $result['msg'] = "添加失败";
                        }
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获取购物车商品数量
     * @queyuan
     * 2016/07/11
     * @param $user_id
     */
    public function getCartNum(){
        $user_id = I("param.user_id");
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $count = M("cart")->where("user_id = ".$user_id." and status = 0")->count();
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['count'] = $count;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获取购物车商品
     * @queyuan
     * 2016/07/12
     * @param $user_id
     */
    public function getUserCart(){
        $user_id = I("param.user_id");
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sales = M("cart c")->join("left join one_sales s on (c.sale_id = s.id) left join one_goods g on (s.good_id = g.id)")->where("c.user_id = ".$user_id." and c.status = 0 and s.sale_num < s.need_num and c.num > 0")->order("c.add_time desc")->field("c.id,c.sale_id,c.user_id,c.num,g.name,g.index_img,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num")->select();
            $money = M("cart c")->join("left join one_sales s on (c.sale_id = s.id) left join one_goods g on (s.good_id = g.id)")->where("c.user_id = ".$user_id." and c.status = 0 and s.sale_num < s.need_num and c.num > 0")->getField("sum(c.num)");
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['sales'] = $sales;
            $result['money'] = $money;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 修改购物车商品数量
     * @queyuan
     * 2016/07/16
     * @param $id,$flag
     */
    public function editCart(){
        $id = I("param.id");
        $flag = I("param.flag");
        if(!$id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sale_id = M("cart")->where("id = ".$id)->getField("sale_id");
            $sale = M("sales")->where("id = ".$sale_id)->field("min_buy,need_num,sale_num,(need_num - sale_num) left_num")->find();
            $cart = M("cart")->where("id = ".$id)->field("num,user_id")->find();
            if($flag){
                $num = $sale['left_num'] - $cart['num'];
                if($num){
                    if($num >= $sale['min_buy']){
                        $data = array('num'=>($cart['num'] + $sale['min_buy']));
                    }else{
                        $data = array('num'=>($cart['num'] + $sale['left_num']));
                    }
                }
            }else{
                if($cart['num'] <= $sale['min_buy']){
                    $data = array('num'=>0);
                }else{
                    $data = array('num'=>($cart['num'] - $sale['min_buy']));
                }
            }
            $res = M("cart")->where("id = ".$id)->save($data);
            if($res){
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['num'] = M("cart")->where("id = ".$id)->getField("num");
                $result['total'] = M("cart")->where("user_id = ".$cart['user_id']." and status = 0 and num != 0")->getField("sum(num)");
                if(!$result['total']){
                    $result['total'] = 0;
                }
            }else{
                $result['status'] = 1;
                $result['msg'] = "请求失败";
            }
        }
        echo json_encode($result);
        exit;
    }
}
?>